package models

import (
	"time"

	"gorm.io/gorm"
)

type TasksDao struct {
	Id         int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	Tasktitle  string    `gorm:"column:tasktitle;type:varchar(100);NOT NULL" json:"tasktitle"`
	Taskintro  string    `gorm:"column:taskintro;type:varchar(1000);NOT NULL" json:"taskintro"`
	Bounty     int       `gorm:"column:bounty;type:int(11);NOT NULL" json:"bounty"`
	Createtime int       `gorm:"column:createtime;type:int(11);NOT NULL" json:"createtime"`
	Enddate    string    `gorm:"column:enddate;type:varchar(30);NOT NULL" json:"enddate"`
	Audit      int       `gorm:"column:audit;type:int(11);NOT NULL" json:"audit"`
	Taken      int       `gorm:"column:taken;type:int(11);NOT NULL" json:"taken"`
	Typeid     int       `gorm:"column:typeid;type:int(11);NOT NULL" json:"typeid"`
	Userid     int       `gorm:"column:userid;type:int(11);NOT NULL" json:"userid"`
	CreatedAt  time.Time `gorm:"column:created_at;type:timestamp" json:"created_at"`
	UpdatedAt  time.Time `gorm:"column:updated_at;type:timestamp" json:"updated_at"`
}

func (s *TasksDao) TableName() string {
	return "tasks"
}

func (s *TasksDao) Find(tx *gorm.DB, pageNo, pageSize int) ([]*TasksDao, error) {
	tx = tx.Model(&s)
	var list []*TasksDao
	if s.Audit != 0 {
		tx = tx.Where("audit = ?", s.Audit)
	}
	if pageNo != 0 && pageSize != 0 {
		tx = tx.Limit(pageSize).Offset(pageSize * (pageNo - 1))
	}
	if err := tx.Order("id desc").Find(&list).Error; err != nil {
		return list, err
	}
	return list, nil
}

func (s *TasksDao) AdminGetDataFind(tx *gorm.DB) ([]*TasksDao, error) {
	tx = tx.Model(&s)
	tx = tx.Where("audit = ?", s.Audit)
	var list []*TasksDao
	if err := tx.Order("id desc").Find(&list).Error; err != nil {
		return list, err
	}
	return list, nil
}

func (s *TasksDao) Update(tx *gorm.DB) error {
	tx = tx.Model(&s)
	if err := tx.Updates(&s).Error; err != nil {
		return err
	}
	return nil
}

func (s *TasksDao) AdminChangeAuditUpdate(tx *gorm.DB) error {
	tx = tx.Model(&s)
	if err := tx.Update("audit", s.Audit).Error; err != nil {
		return err
	}
	return nil
}

func (s *TasksDao) Count(tx *gorm.DB) (int64, error) {
	tx = tx.Model(&s)
	var total int64
	if s.Audit != 0 {
		tx = tx.Where("audit = ?", s.Audit)
	}
	if err := tx.Count(&total).Error; err != nil {
		return total, err
	}
	return total, nil
}

func (s *TasksDao) GetContentLs(tx *gorm.DB, pageNo, pageSize int, typeId, searchValue string) ([]*TasksDao, int64, error) {
	tx = tx.Model(&s)
	var list []*TasksDao
	var total int64
	if s.Audit != 0 {
		tx = tx.Where("audit = ?", s.Audit)
	}
	if typeId != "" && typeId != "0" {
		tx = tx.Where("typeid = ?", typeId)
	}
	if searchValue != "" {
		tx = tx.Where("tasktitle like ? or taskintro like ?", "%"+searchValue+"%", "%"+searchValue+"%")
	}
	if s.Taken != 0 {
		tx = tx.Where("taken = ?", s.Taken)
	} else {
		tx = tx.Where("taken = 0")
	}
	if err := tx.Count(&total).Error; err != nil {
		return nil, total, err
	}
	if pageNo != 0 && pageSize != 0 {
		tx = tx.Limit(pageSize).Offset(pageSize * (pageNo - 1))
	}
	if err := tx.Order("id desc").Find(&list).Error; err != nil {
		return list, total, err
	}
	return list, total, nil
}
